
package com.productmodule.dao;
import com.productmodule.bean.TbProductUnit;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbProductUnitDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbProductUnit tbProductUnit=null;
    private List<TbProductUnit> tbProductUnitList=null;

    public boolean add(TbProductUnit inTbProductUnit){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_ProductUnit(ProductUnitName,ProductUnitSymbol,ProductUnitFormula,ParentProductUnitId,ProductUnitMemo,UserId) values(?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbProductUnit.getProductUnitName());
        pt.setString(2,inTbProductUnit.getProductUnitSymbol());
        pt.setString(3,inTbProductUnit.getProductUnitFormula());
        if(inTbProductUnit.getParentProductUnitId()==0) pt.setNull(4, Types.INTEGER);
        else  pt.setInt(4, inTbProductUnit.getParentProductUnitId());
        pt.setString(5,inTbProductUnit.getProductUnitMemo());
        pt.setInt(6,inTbProductUnit.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inProductUnitId){
        return UtilDAO.del("Tb_ProductUnit", "ProductUnitId", inProductUnitId);
    }
    public boolean edit(TbProductUnit inTbProductUnit){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_ProductUnit set ProductUnitName=?,ProductUnitSymbol=?,ProductUnitFormula=?,ParentProductUnitId=?,ProductUnitMemo=?,UserId=?,CreateTime=getdate() where ProductUnitId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbProductUnit.getProductUnitName());
        pt.setString(2,inTbProductUnit.getProductUnitSymbol());
        pt.setString(3,inTbProductUnit.getProductUnitFormula());
        if(inTbProductUnit.getParentProductUnitId()==0) pt.setNull(4, Types.INTEGER);
        else  pt.setInt(4, inTbProductUnit.getParentProductUnitId());
        pt.setString(5,inTbProductUnit.getProductUnitMemo());
        pt.setInt(6,inTbProductUnit.getUserId());
        pt.setInt(7,inTbProductUnit.getProductUnitId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbProductUnit getTbProductUnitByProductUnitId(int inProductUnitId) {
        tbProductUnit=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductUnit where ProductUnitId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProductUnitId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbProductUnit == null)tbProductUnit = getTbProductUnit(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbProductUnit;
    }
    public List<TbProductUnit> getTbProductUnitAll() {
        tbProductUnitList=new ArrayList<TbProductUnit>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductUnit";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductUnitList.add(getTbProductUnit(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbProductUnitList;
    }
    public List<TbProductUnit> getTbProductUnitAll(VskPage vp) {
        tbProductUnitList=new ArrayList<TbProductUnit>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductUnit";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_ProductUnit", "ProductUnitId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductUnitList.add(getTbProductUnit(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbProductUnitList;
    }
    public TbProductUnit getTbProductUnit(ResultSet rs) throws Exception{
        TbProductUnit tbProductUnit = new TbProductUnit();
        tbProductUnit.setProductUnitId(rs.getInt("ProductUnitId"));
        tbProductUnit.setProductUnitName(rs.getString("ProductUnitName"));
        tbProductUnit.setProductUnitSymbol(rs.getString("ProductUnitSymbol"));
        tbProductUnit.setProductUnitFormula(rs.getString("ProductUnitFormula"));
        tbProductUnit.setParentProductUnitId(rs.getInt("ParentProductUnitId"));
        tbProductUnit.setProductUnitMemo(rs.getString("ProductUnitMemo"));
        tbProductUnit.setUserId(rs.getInt("UserId"));
        tbProductUnit.setCreateTime(rs.getString("CreateTime"));
        return tbProductUnit;
    }
}
